package servlet;

import bean.Hotel;
import bean.Query;
import bean.Rooms;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@WebServlet(name = "QueryServlet", urlPatterns = "/QueryServlet")
public class QueryServlet extends HttpServlet {
    ArrayList<String> roomType;
    ArrayList<String> roomSize;
    ArrayList<String> price;
    ArrayList<String> roomID;
    ArrayList<String> roomName;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        //从result.jsp中获得request对象
        String hotelID = request.getParameter("hotelID");
        if (hotelID!=null){
            hotelID= hotelID.replace("\"", "");
        }
        String hotelName = request.getParameter("hotelName");
        if (hotelName!=null){
            hotelName= hotelName.replace("\"", "");
        }
        System.out.println("Q"+hotelName);
        String hotelAddress = request.getParameter("hotelAddress");
        if (hotelAddress!=null){
            hotelAddress= hotelAddress.replace("\"", "");
        }
        String from = request.getParameter("from");
        Hotel hotelBean;
        HttpSession session = request.getSession(true);
        hotelBean= (Hotel) session.getAttribute("hotelBean");


        //获取当前时间
        String Checkin = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String Checkout = request.getParameter("Checkout");
        if (Checkout != null){
            //从detail.jsp中获得request对象
            Checkin = request.getParameter("Checkin");
            int checkin=Integer.valueOf(Checkin).intValue();
//            System.out.println(checkin+"QS54行");
            int checkout=Integer.valueOf(Checkout).intValue();
            if (checkin-checkout>=0){
                response.sendRedirect("detail.jsp");
            }else if (Checkin.equals("")&&Checkout.equals("")){
                response.sendRedirect("detail.jsp");
            }
            //将查询时间传入JavaBean中，随后传到detail.jsp
            Query query;
            query = (Query) session.getAttribute("query");
//            if (query==null){
                query = new Query();
                query.setCheckin(Checkin);
                query.setCheckout(Checkout);
                session.setAttribute("query",query);
//            }
//            System.out.println("QueryServlet" + Checkin + Checkout);//日志输出查看结果
        }
        if (from !=null){
            if (from.equals("result")){
                //将数据存放进JavaBean中
                if (hotelBean==null){
                    hotelBean = new Hotel();
                    hotelBean.setHotelName(hotelName);
                    hotelBean.setHotelAddress(hotelAddress);
                    hotelBean.setHotelId(hotelID);
                    session.setAttribute("hotelBean", hotelBean);
                    System.out.println( "QueryServlet"+ hotelID);
                }
            } else if (from.equals("index")) {
                hotelBean = new Hotel();
                hotelBean.setHotelName(hotelName);
                hotelBean.setHotelAddress(hotelAddress);
                hotelBean.setHotelId(hotelID);
                session.setAttribute("hotelBean", hotelBean);
            }
        }


        //连接数据库
        try{
            Class.forName("org.mariadb.jdbc.Driver");
            Connection conn= DriverManager.getConnection("jdbc:mariadb://localhost:3306/hotel-management-system","root","123456");
            PreparedStatement stat;
            ResultSet rs;
            Rooms rooms=null;
            try {
                rooms=(Rooms) session.getAttribute("rooms");
//                if (rooms==null){
                    rooms=new Rooms();
                    session.setAttribute("rooms",rooms);
//                }
            }catch (Exception e){}

            hotelID = hotelBean.getHotelName();
//            System.out.println("QS108行"+hotelID+Checkin);
            String roomSql = "select RoomID,RoomName,RoomType,RoomSize,price from rooms where HotelID = '" + hotelID + "' and RoomCondi=1 and RoomID not in (select RoomID from orders where Checkout>='" + Checkin +"' and OrderCondition='成功预订' intersect select RoomID from orders where Checkin<='"+Checkout+"')";
            stat = conn.prepareStatement(roomSql);
            rs = stat.executeQuery();
            rs.beforeFirst();
            if (!rs.next()){
                System.out.println("抱歉，当前已无房间剩余。");
            }

            roomType = new ArrayList<String>();
            roomSize = new ArrayList<String>();
            price = new ArrayList<String>();
            roomID=new ArrayList<>();
            roomName=new ArrayList<>();

            rs.beforeFirst();
            while (rs.next()){
                roomName.add(rs.getString("RoomName"));
                roomID.add(rs.getString("RoomID"));
                roomType.add(rs.getString("RoomType"));
                roomSize.add(rs.getString("RoomSize"));
                price.add(rs.getString("price"));
                System.out.println(rs.getString("RoomName")+rs.getString("RoomID")+rs.getString("RoomType")+"QS125行");
            }
            conn.close();
            rooms.setRoomName(roomName);
            rooms.setRoomId(roomID);
            rooms.setRoomType(roomType);
            rooms.setRoomSize(roomSize);
            rooms.setPrice(price);
            response.sendRedirect("detail.jsp");
        }catch (SQLException | ClassNotFoundException exp){
            System.out.println(exp);
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
